package com.homegoods.login.presenter;

import android.databinding.ObservableField;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.homegoods.login.ApiUtil;
import com.homegoods.login.KGetVerHelper;
import com.homegoods.login.R;
import com.homegoods.login.model.RegisterParams;
import com.homegoods.login.view.RegisterView;
import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.util.MyUtils;
import com.kedll.library.util.StringUtil;
import com.kedll.library.util.ToastUtil;

import io.reactivex.functions.Consumer;

/**
 * 注册Presenter
 *
 * @author QiQi
 * @date 2017/8/9
 */

public class RegisterPresenter extends BasePresenter<RegisterView> {
    /**
     * 手机号
     */
    public final ObservableField<String> strPhoneNumber = new ObservableField<>();
    /**
     * 密码
     */
    public final ObservableField<String> strPwd = new ObservableField<>();
    /**
     * 验证码
     */
    public final ObservableField<String> strVer = new ObservableField<>();

    private final KGetVerHelper kGetVerHelper;

    private String smsId;

    public RegisterPresenter(RegisterView view, KGetVerHelper kGetVerHelper) {
        super(view);
        this.kGetVerHelper = kGetVerHelper;
    }

    /**
     * 下一步
     *
     * @param view
     */
    public void onNextClicked(View view) {
        if (checkRegisterData()) {
            register(view);
        }
    }

    /**
     * 检查登录所需数据的合法性
     *
     * @return
     */
    private boolean checkRegisterData() {
        String phone = strPhoneNumber.get();
        String pwd = strPwd.get();
        String verCode = strVer.get();

        //手机号不能为空
        if (!StringUtil.isPhoneNum(phone)) {
            ToastUtil.showToast(R.string.string_hint_phone);
            return false;
        }

        if (!StringUtil.checkPassword(pwd)) {
            ToastUtil.showToast("请输入6-20位数字，字母");
            return false;
        }
        if (TextUtils.isEmpty(verCode)) {
            ToastUtil.showToast(R.string.input_ver_code);
            return false;
        }
        if (verCode.length() < 6) {
            ToastUtil.showToast("请输入6位验证码");
            return false;
        }


        return true;
    }

    private void register(final View view) {
        this.mView.hideKeyBoard();
        String number = strPhoneNumber.get();
        String pwd = strPwd.get();
        String verCoder = strVer.get();
        mView.showLoad(view.getContext().getString(R.string.register_posting));
        RegisterParams params = new RegisterParams();
        params.setTelephone(number);
        params.setPassword(MyUtils.MD5(pwd, 2));
        params.setAuthCode(verCoder);

        //modify shibo.zheng 19/07/01
        ApiUtil.registerUser(params, s -> {
            mView.hideLoad();
            mView.showMainActivity();
        }, throwable -> {
            mView.hideLoad();
        });
    }

    /**
     * 获取验证码
     *
     * @param view
     */
    public void onGetVerClickListener(View view) {
        if (checkGetVerCodeData()) {
            getVerCode();
        }
    }

    /**
     * 检查获取验证码是数据的合法性
     *
     * @return
     */
    private boolean checkGetVerCodeData() {
        String phone = strPhoneNumber.get();
        if (!StringUtil.isPhoneNum(phone)) {
            ToastUtil.showToast(R.string.string_hint_phone);
            return false;
        }

        return true;
    }

    private void getVerCode() {
        ApiUtil.getCaptcha(strPhoneNumber.get(), jsonElement -> {
            kGetVerHelper.sendSuc();
            smsId = jsonElement.getString("smsId");
        });
    }
}
